﻿//
// JobViewerControl.cs: Represents a job activated by a worker.
//
// Copyright 2010 Michael Goethe
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using S9PalCommon;

namespace S9Pal
{
    /// <summary>
    /// Represents a job activated by a worker.
    /// </summary>
    public partial class JobViewerControl : UserControl
    {
        #region Fields

        private delegate void UpdateLabelText(String text);
        private delegate void UpdateProgressBarValue(int value);

        private Worker worker;

        #endregion

        #region Constructors

        public JobViewerControl(Worker worker)
        {
            InitializeComponent();
            this.worker = worker;

            this.worker.ProgressChanged += new EventHandler(worker_ProgressChanged);
            this.worker.StatusChanged += new EventHandler(worker_StatusChanged);

            workerNameLabel.Text = this.worker.Name;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Worker associated to this job.
        /// </summary>
        public Worker Worker
        {
            get
            {
                return worker;
            }
            private set
            {
                worker = value;
            }
        }

        #endregion

        #region Private Methods
        
        /// <summary>
        /// Update the current item with the specified text.
        /// </summary>
        /// <param name="text">String to show for the current item.</param>
        private void UpdateCurrentItemLabelText(String text)
        {
            currentItemLabel.Text = text;
        }

        /// <summary>
        /// Update the worker name with the specified text.
        /// </summary>
        /// <param name="text">String to show for the worker.</param>
        private void UpdateWorkerNameLabelText(String text)
        {
            workerNameLabel.Text = text;
        }

        /// <summary>
        /// Update the worker status with the specified text.
        /// </summary>
        /// <param name="text">String to show for the worker status.</param>
        private void UpdateWorkerStatusLabelText(String text)
        {
            workerStatusLabel.Text = text;
        }

        /// <summary>
        /// Update the progress bar with the specified value.
        /// </summary>
        /// <param name="value">Value of the progress.</param>
        private void UpdateJobProgressBarValue(int value)
        {
            workerProgressBar.Value = value;
        }

        #endregion

        #region Events

        private void worker_ProgressChanged(object sender, EventArgs e)
        {
            // Get the worker that fired the event and assemble it's currentItem and progress.
            Worker worker = (Worker)sender;
            String currentItem = "Current " + worker.ItemCategory + ": " + worker.CurrentItemName + " (" + worker.CurrentItemNumber + " of " + worker.ItemCount +")";
            int progress = (int)(worker.Progress * workerProgressBar.Maximum);

            // Invoke the methods to update the current item text and job progress.
            BeginInvoke(new UpdateLabelText(UpdateCurrentItemLabelText), new object[1] { currentItem });
            BeginInvoke(new UpdateProgressBarValue(UpdateJobProgressBarValue), new object[1] { progress});
        }

        private void worker_StatusChanged(object sender, EventArgs e)
        {
            // Get the worker that fired the event and a string representation of it's status.
            Worker worker = (Worker)sender;
            String status = System.Enum.GetName(typeof(WorkerStatus), worker.Status) + ".";

            // Invoke the method to update the status text (for cross-threading).
            BeginInvoke(new UpdateLabelText(UpdateWorkerStatusLabelText), new object[1] { status });
        }

        #endregion
    }
}
